package com.example.testproject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;

import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

public class BookManagerActivity extends Activity {
    public static final String TAG = "BookManagerActivity";
    private static final int MESSAGE_NEW_BOOK_AAIVED = 1;
    private IBookManager mRemoteBookManager;
    private Handler mHandler = new Handler(){
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case  MESSAGE_NEW_BOOK_AAIVED:
                    Log.d(TAG,"newbook"+msg.obj);
                    break;
                default:
                    super.handleMessage(msg);
            }

        }
    };
    private ServiceConnection mconnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            IBookManager bookManager = IBookManager.Stub.asInterface(service);
            try {
                mRemoteBookManager = bookManager;
                List<Book> list = bookManager.getBookList();
//                list.add(new Book(3,"android 开发"));
                Book newBook = new Book(3,"android 进阶");
                bookManager.addBook(newBook);
                List<Book> newList = bookManager.getBookList();
                Log.i(TAG,"query book list,list type"+list.getClass());
//                getCan
                Log.i(TAG,"query book list"+newList.toString());
                bookManager.registerListener(mONNewBookArrivedListener);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    private IOneBookArrivedListener mONNewBookArrivedListener = new IOneBookArrivedListener.Stub() {
        @Override
        public void onNewBookArrived(Book newBook) throws RemoteException {
            mHandler.obtainMessage(MESSAGE_NEW_BOOK_AAIVED,newBook).sendToTarget();
        }

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_manager);
        Intent intent = new Intent(this,BookManagerService.class);
        bindService(intent,mconnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        if(mRemoteBookManager != null
                && mRemoteBookManager.asBinder().isBinderAlive()){
            try {
                Log.i(TAG,"解绑观察"+mONNewBookArrivedListener);
                mRemoteBookManager.unregisterListener(mONNewBookArrivedListener);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        unbindService(mconnection);
        super.onDestroy();
    }
}
